package com.comtop.springboot.templet.modules.sys.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.comtop.springboot.templet.modules.sys.service.ISysMenuService;
import com.comtop.springboot.templet.modules.sys.service.ISysUserService;
import com.comtop.springboot.templet.modules.sys.mapper.SysMenuMapper;
import com.comtop.springboot.templet.modules.sys.entity.SysMenuEntity;
import com.comtop.springboot.templet.common.utils.Constant;
import com.comtop.springboot.templet.common.utils.Constant.MenuType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


@Service("sysMenuService")
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper,SysMenuEntity> implements ISysMenuService {
	@Autowired
	private SysMenuMapper sysMenuMapper;
	@Autowired
	private ISysUserService ISysUserService;
	
	@Override
	public List<SysMenuEntity> queryParentIdMenuList(Long parentId, List<Long> menuIdList) {
		List<SysMenuEntity> menuList = queryParentIdMenuList(parentId);
		if(menuIdList == null){
			return menuList;
		}
		
		List<SysMenuEntity> userMenuList = new ArrayList<>();
		for(SysMenuEntity menu : menuList){
			if(menuIdList.contains(menu.getMenuId())){
				userMenuList.add(menu);
			}
		}
		return userMenuList;
	}
	/**
	 * 根据父菜单，查询子菜单
	 * @param parentId 父菜单ID
	 */
	@Override
	public List<SysMenuEntity> queryParentIdMenuList(Long parentId) {
		return sysMenuMapper.queryParentIdMenuList(parentId);
	}
	/**
	 * 获取不包含按钮的菜单列表
	 */
	@Override
	public List<SysMenuEntity> queryNotButtonMenuList() {
		return sysMenuMapper.queryNotButtonMenuList();
	}
	/**
	 * 获取用户菜单列表
	 */
	@Override
	public List<SysMenuEntity> getUserMenuList(Long userId) {
		//系统管理员，拥有最高权限
		if(userId == Constant.SUPER_ADMIN){
			return getAllMenuList(null);
		}
		
		//用户菜单列表
		List<Long> menuIdList = ISysUserService.queryUserAllMenuId(userId);
		return getAllMenuList(menuIdList);
	}
	


	@Override
	public List<SysMenuEntity> queryAllMenuList(Map<String, Object> map) {
		return sysMenuMapper.queryAllMenuList(map);
	}


	@Override
	@Transactional
	public void deleteMenuBatch(Long[] menuIds) {
		sysMenuMapper.deleteMenuBatch(menuIds);
	}

	/**
	 * 获取所有菜单列表
	 */
	private List<SysMenuEntity> getAllMenuList(List<Long> menuIdList){
		//查询根菜单列表
		List<SysMenuEntity> menuList = queryParentIdMenuList(0L, menuIdList);
		//递归获取子菜单
		getMenuTreeList(menuList, menuIdList);
		
		return menuList;
	}

	/**
	 * 递归
	 */
	private List<SysMenuEntity> getMenuTreeList(List<SysMenuEntity> menuList, List<Long> menuIdList){
		List<SysMenuEntity> subMenuList = new ArrayList<SysMenuEntity>();
		
		for(SysMenuEntity entity : menuList){
			if(entity.getType() == MenuType.CATALOG.getValue()){//目录
				entity.setList(getMenuTreeList(queryParentIdMenuList(entity.getMenuId(), menuIdList), menuIdList));
			}
			subMenuList.add(entity);
		}
		
		return subMenuList;
	}
}
